dojo.require("dijit.form.Button");
dojo.require("dijit.form.SimpleTextarea");

var textarea;
var oldwidth;

function togglePreview(id) {
  var button = dijit.byId(id+"_toggle");
  var child = dojo.byId(id);
  var parent = child.parentNode;
  
  var staticOffset = null;

  if (child.nodeName == "TEXTAREA") {
    // È una textarea: significa che va sostituito con il div della preview
    var preview = dojo.create("div");
    dojo.style(preview, {
      "height":dojo.style(child, "height")+"px",
      "overflow":"scroll"
    });

    textarea = child; // Salva la textarea

    parent.replaceChild(preview, child);
    preview.id = id;
    preview.innerHTML = child.value;

    applyZebra();
    fixImages();
    fixLinks();

    child = preview;
    button.setLabel(Drupal.t("Mostra codice"));

    oldwidth = dojo.style(parent.parentNode, "width")+"px";
    dojo.style(parent.parentNode, "width", "100%");
  } else {
    dojo.style(textarea, "height", dojo.style(child, "height") + "px");
    parent.replaceChild(textarea, child);
    
    child = textarea;
    button.setLabel(Drupal.t("Mostra anteprima"));

    dojo.style(parent.parentNode, "width", oldwidth);
  }
  
  dojo.query("div.grippie", parent).onmousedown(startDrag);
  
  function startDrag(e) {
    staticOffset = dojo.style(child, "height") - e.pageY;
    dojo.style(child, 'opacity', 0.25);
    $(document).mousemove(performDrag).mouseup(endDrag);
    return false;
  }

  function performDrag(e) {
    h = Math.max(32, staticOffset + e.pageY) + "px";
    dojo.style(child, "height", h);
    return false;
  }

  function endDrag() {
    $(document).unbind("mousemove", performDrag).unbind("mouseup", endDrag);
    dojo.style(child, 'opacity', 1);
  }
}

var fixImages = function() {
  dojo.query("img[path]").forEach(function(img) {
    img.src = Drupal.settings.basePath + img.getAttribute("path");
  });
};

var fixLinks = function() {
  dojo.query("ref").forEach(function(ref) {
    var a = dojo.create("a");
    for (i = 0; i < ref.attributes.length; i++) {
      a.setAttribute(ref.attributes[i].name, ref.attributes[i].value)
    }
    if (ref.getAttribute("page").substring(0, 4) == "http") {
      a.href = ref.getAttribute("page");
    } else {
      a.href = Drupal.settings.basePath + ref.getAttribute("page");
    }
    a.innerHTML = ref.innerHTML;
    ref.parentNode.replaceChild(a, ref);
  });
};

dojo.addOnLoad(fixImages);
dojo.addOnLoad(fixLinks);

dojo.addOnLoad(function() {
  var field = dojo.byId('edit-lb-editor-qf-title');
  if (field) {
    dojo.connect(field, "onkeydown", function(e) {
      if (13 == e.keyCode) {
        e.preventDefault();
        setTimeout("callSubmit();", 200);
      }
    });
  }
});

function callSubmit() {
  var field = dojo.byId('edit-lb-editor-qf-title');
  dojo.xhrGet({
    url:encodeURI(Drupal.settings.basePath + "/lb_editor/qf_submit/" + field.value),

    load: function(data) {
      if (data == "") return;

      var myField = dojo.byId(dojo.byId('edit-lb-editor-qf-textareaid').value);

      if(document.selection) {
        myField.focus();
        var sel = document.selection.createRange();
        sel.text = data;
      }
      //other browsers
      else if(myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)+ data + myField.value.substring(endPos, myField.value.length);
      } else {
        myField.value += data;
      }
    }
  });
}
